package stuff;

import java.util.Date;
import java.util.ArrayList;

import model.Gender;
import model.TreatmentRecord;
import model.User;

/**
 * This class creates a new CRUDUserTool with no user currently being worked on.
 * (CRUD standing for create, revise, update, delete).
 * 
 * @author Josh Harris
 */

public class CRUDUserTool {
	
	private boolean userExistsInDatabase;

	private User user;
	
	/**
	 * This constructor sets the userExistsInDatabase as false and sets the user to null. 
	 */
	public CRUDUserTool(){
		userExistsInDatabase = false;
		user = null;
	}

	/**
	 * This loads a user from the user database. This does not load a user into
	 * the CRUDUserTool if the user can not be found in the database or the user
	 * that is currently being worked on in the database has been stored into
	 * the database or erased.
	 * 
	 * @return Returns true if user is loaded into CRUDUserTool and false if the
	 *         user is not loaded.
	 */
	public boolean loadUser(){
		if(user == null){
			//user = database.load();
			if(user != null){
				userExistsInDatabase = true;
				return true;
			}
		}
		return false;
	}

	/**
	 * This loads a new User into the CRUDUserTool. This does not create a new
	 * user unless the user that is currently being worked on has been stored in
	 * the database or erased.
	 * 
	 * @return Returns true if a new User is created and false if CRUDUserTool
	 *         could not create a new user.
	 */
	public boolean newUser(){
		if(user == null){
			user = new User();
			userExistsInDatabase = false;
			return true;
		}
		return false;
	}

	/**
	 * This method stores the user that is currently being worked on.
	 * 
	 * @return Returns true if the user was stored into the database or false if
	 *         the user could not be stored into the database or is null.
	 */
	public boolean storeUser(){  // needs more work if user exists in database already
		if(user != null){
			//database.save(user)
			// check if user was stored
			userExistsInDatabase = false;
			user = null;
			return true;
		}
		return false;
	}
	
	/**
	 * This method erases the current user that is being worked on.
	 */
	public void eraseUser(){
		userExistsInDatabase = false;
		user = null;
	}
		
	/**
	 * This method deletes a user from the database.
	 */
	public void deleteUser(){
		// deletes a user from the database
	}
		
	/**
	 *  This method updates the users username by using the setUsername method. 
	 *  
	 *  @param username The new username of the user as a type String.
	 */
	public void updateUsername(String username){
		user.setUserName(username);
	}
	
	/**
	 * This method updates the password by using the setPassowrd method. 
	 * 
	 * @param password The new password of the user as a type String. 
	 */
	public void updatePassword(String password){
		user.setPassword(password);
	}
	
	/**
	 * This method updates the suspended status of the user by taking in a boolean. 
	 * 
	 * @param suspend Takes in a boolean to set the new suspended status of the user.
	 */
	public void updateSuspend(boolean suspend){
		user.setSuspended(suspend);
	}

	/**
	 * This method updates the roles that the user can use to enter the system
	 * (aka doctor, nurse, patient, system admin).
	 * 
	 * @param roles
	 *            The boolean array of length four that represents which roles
	 *            the user can login as.
	 */
	public void updateRoles(boolean[] roles){
		user.setRoles(roles);
	}

	/**
	 * This method updates the name of the user using the setName method of the
	 * user class.
	 * 
	 * @param name
	 *            Takes in a string to set the name.
	 */
	public void updateName(String name){
		user.setName(name);
	}

	/**
	 * This method updates the address of the user using the setAddress method
	 * of the user class.
	 * 
	 * @param address
	 *            Takes in a string called address to set the address to.
	 */
	public void updateAddress(String address){
		user.setAddress(address);
	}

	/**
	 * This method updates the phone of the user using the setPhone method of
	 * the user class.
	 * 
	 * @param phone
	 *            Takes in a string called phone to set the phone to.
	 */
	public void updatePhone(String phone){
		user.setPhone(phone);
	}

	/**
	 * This method updates the cellPhone of the user using the setCellPhone
	 * method of the user class.
	 * 
	 * @param cellPhone
	 *            Takes in a string called cellphone to set the cellphone to.
	 */
	public void updateCellPhone(String cellPhone){
		user.setCellPhone(cellPhone);
	}

	/**
	 * This method updates the email of the user using the setEmail method of
	 * the user class.
	 * 
	 * @param email
	 *            Takes in a string called email to set the email to.
	 */
	public void updateEmail(String email){
		user.setEmail(email);
	}

	/**
	 * This method updates the gender of the user using the setGender method of
	 * the user class.
	 * 
	 * @param gender
	 *            Takes in a string called gender to set the gender to.
	 */
	public void updateGender(Gender gender){
		user.setGender(gender);
	}

	/**
	 * This method updates the birthday of the user using the setBirthdate
	 * method of the user class.
	 * 
	 * @param Date
	 *            Takes in a variable of type Date called date to set the
	 *            birthdate to.
	 */
	public void updateBirthdate(Date date){
		user.setBirthDate(date);
	}

	/**
	 * This method updates the Pharmacy of the user using the setPharmacy method
	 * of the user class.
	 * 
	 * @param pharmacy
	 *            Takes in a string called pharmacy to set the pharmacy to.
	 */
	public void updatePharmacy(String pharmacy){
		user.setPharmacy(pharmacy);
	}

	/**
	 * This method updates the insurace of the user using the setInsurance
	 * method of the user class.
	 * 
	 * @param insurance
	 *            Takes in a string called insurance to set the insurance to.
	 */
	public void updateInsurance(String insurance){
		user.setInsurance(insurance);
	}

	/**
	 * This method updates the allergies of the user using the setAllergies
	 * method of the user class.
	 * 
	 * @param allergies
	 *            Takes in a string called allergies to set the allergies to.
	 */
	public void updateAllergies(String allergies) {
		user.setAllergies(allergies);
	}

	/**
	 * This method reads the user into the CRUD tool.
	 * 
	 * @return Returns the user.
	 */
	public User readUser() {
		return user;
	}

	/**
	 * This method reads the userName into the CRUD tool and calles getUserName
	 * of the user class.
	 * 
	 * @return Returns the user name.
	 */
	public String readUsername() {
		return user.getUserName();
	}

	/**
	 * This method reads the password into the CRUD tool.
	 * 
	 * @return Returns the users password using the getPassword method of user
	 *         class.
	 */
	public String readPassword() {
		return user.getPassword();
	}

	/**
	 * This method reads the suspended status into the CRUD tool.
	 * 
	 * @return Returns the status of the account by using the getSuspended
	 *         method of the user class.
	 */
	public boolean readSuspend() {
		return user.getSuspended();
	}

	/**
	 * This method reads the role into the CRUD tool.
	 * 
	 * @return Returns the role by using the getRoles method of the user class.
	 */
	public boolean[] readRoles() {
		return user.getRoles();
	}

	/**
	 * This method reads the name into the CRUD tool.
	 * 
	 * @return Returns the name by using the getName method of the user class.
	 */
	public String readName() {
		return user.getName();
	}

	/**
	 * This method reads the address into the CRUD tool.
	 * 
	 * @return Returns the address by using the getAddress method of the user
	 *         class.
	 */
	public String readAddress() {
		return user.getAddress();
	}

	/**
	 * This method reads the phone number into the CRUD tool.
	 * 
	 * @return Returns the phone number by using the getPhone method of the user
	 *         class.
	 */
	public String readPhone() {
		return user.getPhone();
	}

	/**
	 * This method reads the cell phone into the CRUD tool.
	 * 
	 * @return Returns the cell phone number by using the getCellPhone method of
	 *         the user class.
	 */
	public String readCellPhone() {
		return user.getCellPhone();
	}

	/**
	 * This method reads the email into the CRUD tool.
	 * 
	 * @return Returns the email by using the getEmail method of the user class.
	 */
	public String readEmail() {
		return user.getEmail();
	}

	/**
	 * This method reads the gender into the CRUD tool.
	 * 
	 * @return Returns the gender by using the getGender method of the user
	 *         class.
	 */
	public Gender readGender() {
		return user.getGender();
	}

	/**
	 * This method reads the birth date into the CRUD tool.
	 * 
	 * @return Returns the birth date by using the getBirthDate method of the
	 *         user class.
	 */
	public Date readBirthDate() {
		return user.getBirthDate();
	}

	/**
	 * This method reads the age into the CRUD tool.
	 * 
	 * @return Returns the age by using the getAge method of the user class.
	 */
	public int readAge() {
		return 0; // user.getAge();
	}

	/**
	 * This method reads the pharmacy into the CRUD tool.
	 * 
	 * @return Returns the pharmacy by using the getPharmacy method of the user
	 *         class.
	 */
	public String readPharmacy() {
		return user.getPharmacy();
	}

	/**
	 * This method reads the insurance into the CRUD tool.
	 * 
	 * @return Returns the insurance by using the getInsurance method of the
	 *         user class.
	 */
	public String readInsurance() {
		return user.getInsurance();
	}

	/**
	 * This method reads the allergies into the CRUD tool.
	 * 
	 * @return Returns the allergies by using the getAllergies method of the
	 *         user class.
	 */
	public String readAllergies() {
		return user.getAllergies();
	}

	/**
	 * This method reads the Treatment Records into the CRUD tool.
	 * 
	 * @return Returns the treatment records by using the getNewestTreatment
	 *         method of the user class.
	 */
	public TreatmentRecord readNewestTreatment() {
		return user.getNewestTreatment();
	}

	/**
	 * This method reads the treatment records into an array into the CRUD tool.
	 * 
	 * @return Returns the treatment records by using the getTreatmentRecords
	 *         method of the user class.
	 */
	public List<TreatmentRecord> readTreatmentRecords() {
		return user.getTreatmentRecords();
	}

}
